package org.apache.ibatis.demo;

import org.apache.ibatis.demo.entity.Blog;
import org.apache.ibatis.demo.mapper.BlogMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/**
 * @fileName: TestDemo
 * @author: zengqf3
 * @date: 2021-3-9 11:47
 * @description:
 */
public class TestDemo3OnDuplicateKey {

  public static void main(String[] args) throws Exception {

    // 加载mybatis配置文件，并创建SqlSessionFactory实例
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    //这个build方法可以接受几种不同的参数，如Reader/InputSteam等
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession = factory.openSession();

    BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
    List<Blog> blogs = Arrays.asList(
      new Blog(101, "222222", "2222"),
      new Blog(107, "107", "107"),
      new Blog(108, "108", "108"));
    int update = mapper.batchInsertOrUpdate(blogs);
    sqlSession.commit();
    System.out.println(update);


  }

}
